package com.infore.airsupervise.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.infore.airsupervise.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 横向滑动选择器
 * Created by xiaotao on 2017/6/20.
 */

public class HorizontalSelectScrollView extends HorizontalScrollView implements CompoundButton.OnCheckedChangeListener {
    private static final int SORT_ASC = 0;
    private static final int SORT_DESC =1 ;

    private Context context;

    private RadioGroup rg_selector;//单选组
    private List<String> factorList; //因子集合
    private FactorChangerListener factorChangerListener;




    /**
     * 选择因子的时间监听
     */
    public  interface FactorChangerListener{
        void onChangeListener(String factor);
    }

    public void setFactorChangerListener(FactorChangerListener listener){
        this.factorChangerListener = listener;
    }


    public HorizontalSelectScrollView(Context context) {
        this(context,null);
    }

    public HorizontalSelectScrollView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HorizontalSelectScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //初始化一个单选组 加入布局
        rg_selector = new RadioGroup(context);
        rg_selector.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        //分割线
        rg_selector.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END |LinearLayout.SHOW_DIVIDER_BEGINNING);
        rg_selector.setDividerDrawable(ContextCompat.getDrawable(context, R.drawable.divider_line_1px));
        //间距
        rg_selector.setDividerPadding(16);
      //  rg_selector.setPadding(0,16,0,16);
        rg_selector.setOrientation(LinearLayout.HORIZONTAL);
        this.addView(rg_selector);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    /**
     * 填充因子
     * @param list
     */
    public void setFactorList(List<String> list){
        factorList = list == null ? new ArrayList<String>(): list;
        if (factorList == null) {
            return;
        }

        for (int i=0; i<factorList.size(); i++) {
            String factor = factorList.get(i);
            RadioButton rb = getRadioButton(factor);
            rg_selector.addView(rb);
            if(i ==0) {
                rb.setChecked(true);
            }
        }
    }



    /**
     * 初始化一个单选按钮
     * @param factor
     * @return
     */
    private RadioButton getRadioButton( String factor){
        RadioButton radioButton = new RadioButton(context);
        radioButton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        //图标
        radioButton.setButtonDrawable(ContextCompat.getDrawable(context,android.R.color.transparent));
        Drawable drawable = ContextCompat.getDrawable(context , R.drawable.ic_factor_radio_selector);
        radioButton.setCompoundDrawablesWithIntrinsicBounds(null,null,null ,drawable);
        radioButton.setCompoundDrawablePadding(16);
        radioButton.setPadding(0,16,0,0);
        //文字
        radioButton.setGravity(Gravity.CENTER_HORIZONTAL);
        radioButton.setText(factor);
        radioButton.setTextSize(14);
        radioButton.setTextColor(ContextCompat.getColorStateList(context,R.color.factor_radio_text));
        //事件监听
        radioButton.setOnCheckedChangeListener(this);
        radioButton.setTag(factor);
        return radioButton ;
    }



    /**
        单选按钮选择状态改变监听
     **/
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            String factor = (String) buttonView.getTag();
            if(factorChangerListener != null){
                factorChangerListener.onChangeListener(factor);
            }
        }
    }

}
